package com.example.crud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.crud.common.ErrorCode;
import com.example.crud.domain.Product;
import com.example.crud.exception.BusinessException;
import com.example.crud.mapper.ProductMapper;
import com.example.crud.service.ProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author gtvtv
* @description 针对表【product】的数据库操作Service实现
* @createDate 2024-07-31 23:05:25
*/
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
    implements ProductService {
    
    @Resource 
    private ProductMapper productMapper;


    @Override
    public long addProduct(String productName, Double price) {
        //参数校验
        if (productName == null || productName.trim().isEmpty()){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (price == null || price < 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //产品名不重复
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productName",productName);
        Long count = productMapper.selectCount(queryWrapper);
        if (count > 0 ){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"产品名重复");
        }
        //插入数据
        Product product = new Product();
        product.setProductName(productName);
        product.setPrice(price);
        productMapper.insert(product);
        return product.getId();
    }

    @Override
    public void delete(Long id) {
        //参数校验
        if (id == null || id < 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //查询产品
        Product product = productMapper.selectById(id);
        if (product == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"产品不存在");
        }
        //删除产品
        int i = productMapper.deleteById(id);
    }

    @Override
    public long update(String productName, Double price) {
        //参数校验
        if (productName == null || productName.trim().isEmpty()){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (price == null || price < 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //查询产品
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productName",productName);
        Product oldProduct = productMapper.selectOne(queryWrapper);
        if (oldProduct == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"产品不存在");
        }
        //更新数据
        Product product = new Product();
        product.setProductName(productName);
        product.setPrice(price);
        productMapper.updateById(product);
        return product.getId();
    }
    

    @Override
    public Product select(String productName) {
        //查询产品
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productName",productName);
        Product product = productMapper.selectOne(queryWrapper);
        if (product == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"产品不存在");
        }
        return product;
    }

    @Override
    public Page<Product> selectPage(int page, int size) {
        Page selectPage = productMapper.selectPage(new Page(page,size),null);
        return selectPage;
    }
}




